import { createStore } from 'vuex'
import createPersistedState from 'vuex-persistedstate'

export default createStore({
  state: {
    isCollapse1: false,
    // isCollapse: false,
    navMenuList: [],
    defaultTab: "",
    defaultPath: "",
    indexPath: "",
    indexTab: "",
    defaultPath0: '',
  },
  mutations: {
    addMenuHandle(state, val) {
      if (val.label == "首页") {
        state.indexTab = val.id
        state.indexPath = val.path
      }
      state.defaultPath = val.path
      state.defaultPath0 = val.path
      const arr = state.navMenuList.filter(item => item.id == val.id)
      state.defaultTab = val.id

      if (arr.length == 0) {
        state.navMenuList.push(val)
      } else {
        return
      }
    },
    delMenuHandle(state, val) {
      state.navMenuList = state.navMenuList.filter(item => {
        return item.id != val
      })
    },
    toggleHandle(state, val) {
      state.isCollapse1 = val
    },
    toggleTabId(state, val) {
      state.defaultTab = val
      if (state.navMenuList.length > 0) {
        const pathData = state.navMenuList.filter(item => item.id == val)
        state.defaultPath = pathData[0].path
      }
    },
    toggleDelTabId(state, val) {
      if (state.navMenuList.length > 0) {
        if (state.defaultTab == val) {
          state.defaultTab = state.navMenuList[state.navMenuList.length - 1].id
          state.defaultPath = state.navMenuList[state.navMenuList.length - 1].path
          state.defaultPath0 = state.navMenuList[state.navMenuList.length - 1].path
        }
      } else {
        state.defaultTab = state.indexTab
        state.defaultPath = state.indexPath
        state.defaultPath0 = state.indexPath
      }
    },
  },
  actions: {
    uploadAdd({ commit }, value) {
      console.log(2222, value);
      commit('addMenuHandle', value);
    },
    uploadDel({ commit }, value) {
      commit('delMenuHandle', value);
    },
    uploadtoggle({ commit }, value) {
      commit('toggleHandle', value);
    },
    uploadTabId({ commit }, value) {
      commit('toggleTabId', value);
    },
    uploadDelTabId({ commit }, value) {
      commit('toggleDelTabId', value);
    },
  },
  getters: {
    // includesValue: state => value => state.navMenuList.find(item => item.id == value) !== undefined
  },
  modules: {
  },
  plugins: [
    createPersistedState({
      storage: window.sessionStorage, // 你可以选择使用 sessionStorage 或其他存储方式  
    }),
  ],
})
